﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using Microsoft.SharePoint.Administration;

namespace MicroLink.StsDev2008AddIn.BuildTasks
{
    public class DeleteSolution : Task
    {
        #region SolutionName
        [Required()]
        public string SolutionName
        {
            get;
            set;
        }
        #endregion

        public override bool Execute()
        {
            SPSolution solution = SPFarm.Local.Solutions[SolutionName];
            if (solution != null)
            {
                if (solution.Deployed)
                {
                    Log.LogError("{0} is deployed and cannot be deleted.", SolutionName);
                    return false;
                }

                solution.Delete();
                return true;
            }
            else
            {
                Log.LogError("{0} does not exist in the solution store", SolutionName);
                return false;
            }
        }
    }
}
